<?php
/**
 * @author     Thibaut Cromphaut, Gaïtano Boeraeve, Matthias Mercy
 * @copyright  Copyright (c) 2011 memorygame
 */
class Application_Model_UserHasLevelMapper extends Ahs_Model_Mapper
{

    public function __construct(array $options = null)
    {
        parent::__construct($options);
        $this->DbTableName = 'Application_Model_DbTable_UserHasLevels';
    }
    
    /**
     * Het eerste element ophalen
     * 
     * @param integer $id
     * @return Application_Model_Level
     */
    /*public function fetchFirstLevelForWorld($levelId, $userId)
    {
        $row = $this->getDbTable()->fetchRow(array('level_id = ?'  => $levelId, 'user_id = ?'  => $userId));

        //return $this->_toUserHasLevel($row);
        
        Zend_Debug::dump($this->_toUserHasLevel($row));
        die("this");
    }*/
    
    /**
     * Level opslaan
     *
     * @param Application_Model_UserHasLevel $userHasLevel 
     */
    public function save(Application_Model_UserHasLevel $userHasLevel)
    {
        $data = array(            
            'level_id'              => $userHasLevel->Id,           
            'user_id'               => $userHasLevel->UserId,
            'userHasLevel_score'    => $userHasLevel->Score,
            'userHasLevel_time'     => $userHasLevel->Time,
            'userHasLevel_tries'    => $userHasLevel->Tries,           
            'userHasLevel_unlocked' => $userHasLevel->Unlocked,  
        );        

        //if (null == $userHasLevel->Id) {
            //$this->getDbTable()->insert($data); //$userHasLevel->Id         $userHasLevel->UserId
        //} else {  
            $this->getDbTable()->update($data, array('level_id = ?' => $userHasLevel->Id, 'user_id = ?' => $userHasLevel->UserId));
        //}
    }    
    
    
    private function _toUserHasLevel(Zend_Db_Table_Row_Abstract $row = null)
    {
        $userHasLevel = new Application_Model_UserHasLevel();

        if ($row) {
            $userHasLevel->Id           = $row['level_id'];
            $userHasLevel->UserId       = $row['user_id'];
            $userHasLevel->Score        = $row['userHasLevel_score'];
            $userHasLevel->Time         = $row['userHasLevel_time'];
            $userHasLevel->Tries        = $row['userHasLevel_tries'];
            $userHasLevel->Unlocked     = $row['userHasLevel_unlocked'];
        }

        return $userHasLevel;
    }
}


